<?php
  header('content-type:text/html;charset=utf-8;');

  //连接数据库
  function connectDB($host,$username,$pwd,$dbname){
    return $conn = mysqli_connect($host,$username,$pwd,$dbname);
  };

  //关闭数据库
  function closedb($conn){
    mysqli_close($conn);
    echo 'db closed!';
  }

  // 读取数组函数
  function read_arr($dataArr){
    $arrlength=count($dataArr);
    for($x=0;$x<$arrlength;$x++){
        print_r($dataArr[$x]);
        echo "<br>";
    }
  };

  // 执行查询语句
function exec_query($conn,$sql){
    $res = mysqli_query($conn,$sql);
    $arr = array();
    while ($row = mysqli_fetch_assoc($res)) {
        array_push($arr, $row);
    }    
    read_arr($arr);
}

  /* 执行create创建 update更新 delete删除操作 */
    function exec_cud($conn,$sql){
      if($res = mysqli_query($conn,$sql)){
        print_r($res);
      }else{
          echo 'mysql error:'.mysqli_error($conn);
      }
    }
?>